/*
 * Script to manage the admin console
 * 
 * Author: Chris Casola, Dan Robertson
 */

window.onload = function() {
	window.comm = new Communicator();
	window.comm.onmessage = handleResponse;
	window.comm.onerror = function (res) { console.log("Error: " + JSON.stringify(res)); };
	window.comm.start();
	sendReportRequest();
	document.getElementById('removeEvent').addEventListener('click', getSelectedAndSend, false);
	document.getElementById('closeMultiple').addEventListener('click', sendForceCloseRequest, false);
	document.getElementById('showEvents').addEventListener('click',sendReportRequest, false);
};
/**handleResponse is triggered whenever the generate requests button is clicked. It checks the completed fields with the 
 * status of the complete/incomplete check box in order to present relevant events.
 * 
 */
function handleResponse(response) {	
	
	var response = response[0];
	
		
	if (typeof(response.reportResponse) != "undefined") {
		
		clearOptions();
		var eventSelect = document.getElementById('eventList');
		
		if (response.success == "true" && (typeof(response.reportResponse.entry) != "undefined")) {
			var eventList = response.reportResponse.entry;
		
			for (var i=0; i<eventList.length; i++) {
				
				if(document.getElementById('complete').checked == true && eventList[i].completed == "true"){
					var newOption = document.createElement("option");
					newOption.setAttribute("value", eventList[i].id);
					newOption.setAttribute("completed", eventList[i].completed)
					newOption.innerHTML = "ID: " + eventList[i].id + "   Created: " + eventList[i].created + "   Type: " + eventList[i].type + "   Completed: " + eventList[i].completed + "   Question: " + eventList[i].question;
					eventSelect.appendChild(newOption);
				}
				
				if(document.getElementById('incomplete').checked == true && eventList[i].completed == "false"){
					var newOption = document.createElement("option");
					newOption.setAttribute("value", eventList[i].id);
					newOption.setAttribute("completed", eventList[i].completed)
					newOption.innerHTML = "ID: " + eventList[i].id + "   Created: " + eventList[i].created + "   Type: " + eventList[i].type + "   Completed: " + eventList[i].completed + "   Question: " + eventList[i].question;
					eventSelect.appendChild(newOption);
				}
			}
		}
	}

	if (typeof(response.forceResponse) != "undefined"){
		sendReportRequest();
		window.location.reload();
	}
	
}

function sendReportRequest() {
	var request1 = {
		__XMLclass : "reportRequest",
		key : sessionStorage['adminKey'],
		type : "open", 
	};
	var request2 = {
		__XMLclass : "reportRequest",
		key : sessionStorage['adminKey'],
		type : "closed", 
	};
	window.comm.send("/do", [new xmlRequest(request1), new xmlRequest(request2)]);
}
/** sends a delete request for the selected event(s), 
 * and removes them from the list
 */
function getSelectedAndSend(){
	
	var options = document.getElementById('eventList');
	for (i = 0 ; i < options.length ; i++){
		
		if(options.children[i].selected == 1){

			sendRemoveEventRequest(options.children[i].value);
			options.remove(i);
		}
	}
	
}

function clearOptions(){
	
	var options = document.getElementById('eventList');
	
	for (i = 0 ; i < options.length ; i++){
			options.remove(i);
		}		
}
/**
 * sends a removeEventRequest to delete an event
 * @param eid the event id
 */

function sendRemoveEventRequest(eid){
	
	var request = {
		__XMLclass : "removeRequest",
		key : sessionStorage['adminKey'],
		id : eid,
	};
	
	window.comm.send("/do", [new xmlRequest(request)]);
}


/**
 * sends a forceCloseRequest to force complete all incomplete events that are more then
 * a desired amount of days.
 */
function sendForceCloseRequest(){
	
	var request = {
		__XMLclass : "forceRequest",
		key : sessionStorage['adminKey'],
		daysOld : document.getElementById('daysOld').value,
	};
	window.comm.send("/do", [new xmlRequest(request)]);
}
/*
 * Wrap the given request in a generic request
 * and set the request id.
 */
function xmlRequest(innerRequest) {
	this.__XMLclass = "request";
	this.version = "1.0";
	this.id = uuid.v4();
	this.__XMLchildren = [
		innerRequest
	];
}
